package org.projsquirrel;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;


public class CommentsActivity extends ListActivity {
	
	private ListView lv;
	String FILENAME = "data_file";
	ArrayList comments = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        comments = new ArrayList(Arrays.asList(getResources().getStringArray(R.array.comments_array)));
        setContentView(R.layout.comments);
        lv = getListView();
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, comments));
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
        		TextView title = (TextView) view;
        		Intent intent = new Intent(CommentsActivity.this,FullComment.class);
        		Bundle bundle = new Bundle();
        		bundle.putString("title", title.getText().toString() );
        		intent.putExtras(bundle);
        		View v = CommentsGroup.group.getLocalActivityManager()  
                .startActivity("show_fullcomment", intent  
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
                .getDecorView();  
          
                // Again, replace the view  
                CommentsGroup.group.replaceView(v);  
            }
          });
       
        final Button button = (Button) findViewById(R.id.footer);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	Intent intent = new Intent(CommentsActivity.this,Email.class);
            	View view = CommentsGroup.group.getLocalActivityManager()  
                .startActivity("show_emailcomment", intent  
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
                .getDecorView();  
          
                // Again, replace the view  
                CommentsGroup.group.replaceView(view);   
            }
        });
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.comment_menu, menu);
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.menucomment:
	        showUpdatePage();
	        return true;
	    case R.id.help:
	        showHelp();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	private void showUpdatePage() {
		Intent intent = new Intent(CommentsActivity.this,ShareComment.class);
		View view = CommentsGroup.group.getLocalActivityManager()  
        .startActivity("show_updatepage", intent  
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
        .getDecorView();  
  
        // Again, replace the view  
        CommentsGroup.group.replaceView(view);   
	}
	
	private void showHelp() {
		Intent intent = new Intent(CommentsActivity.this,About.class);
    	//CommentsActivity.this.startActivity(intent); 
     
           // Create the view using FirstGroup's LocalActivityManager  
           View view = CommentsGroup.group.getLocalActivityManager()  
           .startActivity("show_about", intent  
           .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
           .getDecorView();  
     
           // Again, replace the view  
           CommentsGroup.group.replaceView(view);  
	}
	
}
